﻿using System;
using Platformer_Example.Content.Images.Player;
using Platformer_Example.Controller.Input;
using Transformable_Engine_v2.Engine.Animation.FrameAnimation;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Platformer_Example.Controller.Character.Player
{
    public partial class Player
    {
        protected override void OnKeyPressPickup(InputState.Key key)
        {
            
        }

        protected override void OnKeyReleasePickup(InputState.Key key)
        {
            
        }

        static readonly TE_Rectangle _collisionPickup = new TE_Rectangle(10, -17, 0, 17);
        protected override TE_Rectangle CollisionPickup
        {
            get
            {
                return _collisionPickup;
            }
        }

        private Timer _pickupTimer;
        private FrameAnimation _pickupAnimation;
        protected override void CreatePickupStateObjects()
        {
            AddTimers(_pickupTimer = new Timer(1, false, EndPickup));
        }

        private void EndPickup()
        {
            ChangeState(State.Idle);
        }

        protected override void InitializePickupState()
        {
            var pickupAnimation = new PlayerAnimations.Normal.Pickup();
            _pickupTimer.Reset(pickupAnimation.Duration);
            Character.AddAnimations(_pickupAnimation = new FrameAnimation(pickupAnimation));
            _pickupAnimation.OnChangeFrame += OnChangePickupFrame;

            if(CurrentDirection == Direction.Right)
                Character.OriginAbsolute = new TE_Vector2(8, 32);
            else
                Character.OriginAbsolute = new TE_Vector2(16, 32);

            OnChangePickupFrame(0);
        }

        private void OnChangePickupFrame(int frameid)
        {
            TE_Vector2 carriedObjectPosition = null;
            switch (frameid)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                    carriedObjectPosition = new TE_Vector2(16, 0);
                    break;
            }
            CarriedObject.Position = CurrentDirection == Direction.Right ? carriedObjectPosition : carriedObjectPosition.FlipHorizontally;
        }

        protected override void UpdatePickupState(TimeSpan gameTime)
        {
            
        }

        protected override void EndPickupState()
        {
            _pickupAnimation.OnChangeFrame -= OnChangePickupFrame;
        }
    }
}